﻿package tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
/**
 * NTCIR Corpus File Reader<br>
 * Function: Read a file into a string with structure fully preserved
 * @author BlueJade
 * @version 1.1
 */
public class FileReaderEnhance {
	public static String readToString(File file, String encoding) {
		Long filelength = file.length();
		byte[] filecontent = new byte[filelength.intValue()];
		try {
			FileInputStream in = new FileInputStream(file);
			in.read(filecontent);
			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			return new String(filecontent, encoding);
		} catch (UnsupportedEncodingException e) {
			System.err.println("The OS does not support " + encoding);
			e.printStackTrace();
			return null;
		}
	}
	
	public static String readToString(File file){
		Long filelength = file.length();
		byte[] filecontent = new byte[filelength.intValue()];
		try {
			FileInputStream in = new FileInputStream(file);
			in.read(filecontent);
			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			return new String(filecontent, "GB18030");
		} catch (UnsupportedEncodingException e) {
			System.err.println("The OS does not support GB18030!");
			e.printStackTrace();
			return null;
		}
	}
	
	public static String readToString(String filename) {
		return readToString(new File(filename));
	}
	
	public static String readToString(String filename, String encoding) {
		return readToString(new File(filename), encoding);
	}
	
	public static void main(String[] args) {
		File file = new File("D:\\Projects\\Java\\eclipse3.5_workspace\\NTCIR8\\corpus\\7\\TrainingReleased_CS\\Annotated-UNICODE\\N01-1\\XIN_CMN_20000206_0099.stno");
		System.out.println(FileReaderEnhance.readToString(file, "UTF-16"));

	}
	
}
